<?php
class anggotaMod extends MY_Model{
	
	function __construct(){
		parent::__construct(
			'anggota',
			array(
				'id_anggota',
				'identity',
				'name',
				'place_of_birth',
				'birthday',
				'gender',
				'email',
				'website',
				'phone',
				'address',
				'region',
				'city',
				'blood',
				'education',
				'work',
				'skill',
				'achievement',
				'posted'
			),
			'id_anggota'
		);
	}
	
	function generateID($data){
		return do_hash($data.(unix_to_human(time(),true,'eu')).$this->table_name);
	}
	
	function add($newData){
//		$newData['privacy'] = "0,1,1,1,1,1,1,1,1,1,1,1,1,1";
		$newData['posted'] = unix_to_human(time(),true,'eu');
		$this->db->insert($this->table_name, $newData);
	}
	
	function update($id,$newData){
		$newData['posted'] = unix_to_human(time(),true,'eu');
		$this->db->where($this->table_fields[0], $id);
		return $this->db->update($this->table_name, $newData); 
	}
	
	function delete($ids){
		if (!is_array($ids)) $ids = array($ids);
    	foreach($ids as $pk) {
    		$this->db->or_where($this->table_fields[0], $pk);	
    	}    	
    	return $this->db->delete($this->table_name, $pk);
	}
	
	function remove($ids){
    	return $this->db->delete($this->table_name, array('id_anggota'=>$ids));
    }
	
 	function get($id){
		return $this->db->get_where($this->table_name, array($this->table_fields[0]=>$id));
    }
    
	function get_record(){
//		return $this->db->get($this->table_name, $limit, $offset);
		return $this->db->query("select * from $this->table_name order by posted desc");
    }
    
	function get_records($limit,$offset=20){
//		return $this->db->get($this->table_name, $limit, $offset);
		return $this->db->query("select * from $this->table_name limit $limit, $offset");
    }
    
	function get_record_where($where,$limit,$offset=20){
    	return $this->db->get_where($this->table_name, $where, $limit, $offset);
    }
    
	function get_records_where($where,$limit,$offset){
		return $this->db->query("select * from $this->table_name where identity like '%$where%' or name LIKE '%$where%' or place_of_birth LIKE '%$where%' or gender LIKE '%$where%' or address LIKE '%$where%' or region LIKE '%$where%'  or city LIKE '%$where%'  or education LIKE '%$where%' or work LIKE '%$where%' or skill LIKE '%$where%' limit $limit, $offset");
    	//return $this->db->get_where($this->table_name, $where, $limit, $offset);
    }
	
}